package com.youkeyi.ddy.cloud.application.business.crm.doctor.controller;

import com.youkeyi.ddy.cloud.common.api.application.business.crm.doctor.BusinessDoctorCloudService;
import com.youkeyi.ddy.cloud.common.api.application.business.crm.doctor.dto.BusinessDoctorListCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.business.crm.doctor.dto.BusinessDoctorListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.business.crm.doctor.param.BusinessDoctorCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.business.crm.doctor.param.BusinessDoctorDeleteCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.business.crm.doctor.param.BusinessDoctorListCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.business.crm.doctor.param.BusinessDoctorUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.doctor.CrmDoctorCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.crm.doctor.dto.CrmDoctorListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.crm.doctor.param.CrmDoctorCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.doctor.param.CrmDoctorDeleteCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.doctor.param.CrmDoctorQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.doctor.param.CrmDoctorUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.context.YoukeyiSystemContext;
import com.youkeyi.ddy.cloud.common.enums.ResponseCodeEnum;
import com.youkeyi.ddy.cloud.common.response.ResponseContent;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * BusinessDoctorController
 *
 * @Author liuxiawang
 * @Date 2024/3/12 5:29 PM
 * @Location shenzhen.china
 */
@Service
public class BusinessDoctorController implements BusinessDoctorCloudService {

    @Autowired
    private CrmDoctorCloudService crmDoctorCloudService;

    @Override
    public BusinessDoctorListCloudDto list(BusinessDoctorListCloudParam param) {
        BusinessDoctorListCloudDto dto = new BusinessDoctorListCloudDto();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            dto.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return dto;
        }
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        CrmDoctorQueryCloudParam doctorQueryCloudParam = new CrmDoctorQueryCloudParam();
        doctorQueryCloudParam.setDoctorPrimaryKeys(param.getDoctorPrimaryKeys());
        doctorQueryCloudParam.setCorpKeys(Collections.singletonList(corpKey));
        doctorQueryCloudParam.setCodes(param.getCodes());
        doctorQueryCloudParam.setNameFuzzy(param.getNameFuzzy());
        doctorQueryCloudParam.setPhoneFuzzy(param.getPhoneFuzzy());
        doctorQueryCloudParam.setPageSize(param.getPageSize());
        doctorQueryCloudParam.setCurrentPage(param.getCurrentPage());
        CrmDoctorListCloudDto doctorDtos = crmDoctorCloudService.query(doctorQueryCloudParam);
        if (null != doctorDtos && CollectionUtils.isNotEmpty(doctorDtos.getRows())) {
            List<BusinessDoctorListItemCloudDto> items = new ArrayList<>();
            doctorDtos.getRows().forEach(doctor -> {
                BusinessDoctorListItemCloudDto item = new BusinessDoctorListItemCloudDto();
                item.setDoctorPrimaryKey(doctor.getDoctorPrimaryKey());
                item.setCorpKey(doctor.getCorpKey());
                item.setCode(doctor.getCode());
                item.setName(doctor.getName());
                item.setPhone(doctor.getPhone());
                item.setAddress(doctor.getAddress());
                item.setDesc(doctor.getDesc());
                item.setRemark(doctor.getRemark());
                item.setIsDeleted(doctor.getIsDeleted());
                item.setIsDeletedName(doctor.getIsDeletedName());
                item.setCreateUserKey(doctor.getCreateUserKey());
                item.setCreateUserName(doctor.getCreateUserName());
                item.setCreateTime(doctor.getCreateTime());
                item.setUpdateUserKey(doctor.getUpdateUserKey());
                item.setUpdateUserName(doctor.getUpdateUserName());
                item.setUpdateTime(doctor.getUpdateTime());
                items.add(item);
            });
            dto.setRows(items);
            dto.setTotal(doctorDtos.getTotal());
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public ResponseContent create(BusinessDoctorCreateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param
                || StringUtils.isBlank(param.getName())
        ) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        CrmDoctorCreateCloudParam doctorCreateCloudParam = new CrmDoctorCreateCloudParam();
        doctorCreateCloudParam.setCorpKey(corpKey);
        doctorCreateCloudParam.setCode(param.getCode());
        doctorCreateCloudParam.setName(param.getName());
        doctorCreateCloudParam.setPhone(param.getPhone());
        doctorCreateCloudParam.setAddress(param.getAddress());
        doctorCreateCloudParam.setDesc(param.getDesc());
        doctorCreateCloudParam.setRemark(param.getRemark());
        doctorCreateCloudParam.setCreateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        doctorCreateCloudParam.setCreateUserName(YoukeyiSystemContext.getUserName());
        return crmDoctorCloudService.create(doctorCreateCloudParam);
    }

    @Override
    public ResponseContent update(BusinessDoctorUpdateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param
                || StringUtils.isBlank(param.getDoctorPrimaryKey())
        ) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        CrmDoctorUpdateCloudParam doctorUpdateCloudParam = new CrmDoctorUpdateCloudParam();
        doctorUpdateCloudParam.setDoctorPrimaryKey(param.getDoctorPrimaryKey());
        doctorUpdateCloudParam.setCode(param.getCode());
        doctorUpdateCloudParam.setName(param.getName());
        doctorUpdateCloudParam.setPhone(param.getPhone());
        doctorUpdateCloudParam.setAddress(param.getAddress());
        doctorUpdateCloudParam.setDesc(param.getDesc());
        doctorUpdateCloudParam.setRemark(param.getRemark());
        doctorUpdateCloudParam.setUpdateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        doctorUpdateCloudParam.setUpdateUserName(YoukeyiSystemContext.getUserName());
        return crmDoctorCloudService.update(doctorUpdateCloudParam);
    }

    @Override
    public ResponseContent delete(BusinessDoctorDeleteCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param || CollectionUtils.isEmpty(param.getDoctorPrimaryKeys())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        CrmDoctorDeleteCloudParam doctorDeleteCloudParam = new CrmDoctorDeleteCloudParam();
        doctorDeleteCloudParam.setDoctorPrimaryKeys(param.getDoctorPrimaryKeys());
        doctorDeleteCloudParam.setUpdateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        doctorDeleteCloudParam.setUpdateUserName(YoukeyiSystemContext.getUserName());
        return crmDoctorCloudService.delete(doctorDeleteCloudParam);
    }
}
